package com.liushuai.phoneguardian.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.liushuai.phoneguardian.R;
import com.liushuai.phoneguardian.domain.UpdateInfo;
import com.liushuai.phoneguardian.engine.UpdateInfoParser;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class CheckUpdateUtil implements Runnable {

	public static final int PARSE_XML_ERROR = 10;
	public static final int PARSE_XML_SUCCESS = 11;
	public static final int SERVER_ERROR = 12;
	public static final int URL_ERROR = 13;
	public static final int NETWORK_ERROR = 14;
	public static final int NOT_UPDATE = 20;
	private static final String TAG = "CheckUpdateUtil";
	private Handler handler;
	private Context context;
	private UpdateInfo updateInfo;

	public CheckUpdateUtil(Handler handler, Context context) {
		super();
		this.handler = handler;
		this.context = context;
		
	}

	public void run() {
		
		SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		boolean b = sp.getBoolean("update", true);
		if(!b){
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Message mes = Message.obtain();
			mes.what=NOT_UPDATE;
			handler.sendMessage(mes);
			return;
		}
		long startTime = System.currentTimeMillis();
		Message msg = Message.obtain();
		try {
			URL url = new URL(context.getResources().getString(
					R.string.serverurl));
			Log.i(TAG, "url: " + url);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(1500);
			int type = conn.getResponseCode();
			if (type == 200) {
				InputStream is = conn.getInputStream();
				updateInfo = UpdateInfoParser.getUpdateInfo(is);

				if (updateInfo != null) {
					
					msg.what = PARSE_XML_SUCCESS;
					Bundle bundle = new Bundle();
					bundle.putSerializable("updateinfo", updateInfo);
					msg.setData(bundle);
					
				} else {
					msg.what = PARSE_XML_ERROR;
				}

			} else {
				// TODO:服务器内部错误.
				msg.what = SERVER_ERROR;
			}

		} catch (MalformedURLException e) {
			msg.what = URL_ERROR;
			e.printStackTrace();
		} catch (NotFoundException e) {
			msg.what = URL_ERROR;
			e.printStackTrace();
		} catch (IOException e) {
			msg.what = NETWORK_ERROR;
			e.printStackTrace();
		} finally {
			long endTime = System.currentTimeMillis();
			long dTime = endTime - startTime;
			if (dTime < 2000) {
				try {
					Thread.sleep(2000 - dTime);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			handler.sendMessage(msg);
		}
	}

}
